package io.renren.modules.sys.util;


import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author csl
 * @create 2020-04-21 10:27
 */
public class PdfUtil {


    /**
     * 获取license
     *
     * @return
     */
    public static boolean getLicenseaspose() {
        boolean result = false;
        try {
            InputStream is = PdfUtil.class.getClassLoader().getResourceAsStream("License.xml");
            com.aspose.slides.License aposeLic = new com.aspose.slides.License();
            aposeLic.setLicense(is);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    /**
     * 获取license
     *
     * @return
     */
    public static boolean getLicenseexcel() {
        boolean result = false;
        try {
            InputStream is = PdfUtil.class.getClassLoader().getResourceAsStream("License.xml");
            com.aspose.cells.License aposeLic = new com.aspose.cells.License();
            aposeLic.setLicense(is);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    /**
     * 获取license
     *
     * @return
     */
    public static boolean getLicensewords() {
        boolean result = false;
        try {
            InputStream is = PdfUtil.class.getClassLoader().getResourceAsStream("License.xml");
            com.aspose.words.License aposeLic = new com.aspose.words.License();
            aposeLic.setLicense(is);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * @param wordPath 需要被转换的word全路径带文件名
     * @param pdfPath 转换之后pdf的全路径带文件名
     */
    public static boolean doc2pdf(String wordPath, String pdfPath) {
//        if (!getLicensewords()) { // 验证License 若不验证则转化出的pdf文档会有水印产生
//            return false;
//        }
        try {
            long old = System.currentTimeMillis();
            File file = new File(pdfPath); //新建一个pdf文档
            FileOutputStream os = new FileOutputStream(file);
            com.aspose.words.Document document = new com.aspose.words.Document(wordPath); //Address是将要被转化的word文档
            document.save(os, com.aspose.words.SaveFormat.PDF);//全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
            long now = System.currentTimeMillis();
            os.close();
            System.out.println("共耗时：" + ((now - old) / 1000.0) + "秒"); //转化用时
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        return false;
        }
    }

    /**
     * @param excelPath 需要被转换的excel全路径带文件名
     * @param pdfPath 转换之后pdf的全路径带文件名
     */
    public static boolean excel2pdf(String excelPath, String pdfPath) {
//        if (!getLicenseexcel()) { // 验证License 若不验证则转化出的pdf文档会有水印产生
//            return false;
//        }

        try {
            long old = System.currentTimeMillis();
            com.aspose.cells.Workbook wb = new com.aspose.cells.Workbook(excelPath);// 原始excel路径
            FileOutputStream fileOS = new FileOutputStream(pdfPath);

            wb.save(fileOS, com.aspose.cells.SaveFormat.PDF);

            long now = System.currentTimeMillis();
            System.out.println("共耗时：" + ((now - old) / 1000.0) + "秒");
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    /**
     * aspose:ppt转pdf
     * @param inPath 需要被转换的excel全路径带文件名
     * @param outPath 转换之后pdf的全路径带文件名
     */
    public static boolean ppt2pdf(String inPath, String outPath) {

        // 验证License
//        if (!getLicenseaspose()) {
//            return false;
//        }
        FileOutputStream fileOS = null ;
        try {
            long old = System.currentTimeMillis();
            File file = new File(outPath);// 输出pdf路径
            com.aspose.slides.Presentation pres = new com.aspose.slides.Presentation(inPath);//输入pdf路径
            fileOS = new FileOutputStream(file);
            pres.save(fileOS, com.aspose.slides.SaveFormat.Pdf);
            fileOS.close();

            long now = System.currentTimeMillis();
            System.out.println("共耗时：" + ((now - old) / 1000.0) + "秒\n\n" + "文件保存在:" + file.getPath()); //转化过程耗时
            return true;
        } catch (Exception e) {
            if(fileOS != null) {
                try {
                    fileOS.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            e.printStackTrace();
            return false;
        }



    }


    public static void main(String[] args) {

        //word 和excel 转为pdf
        String filePaths="C:\\Users\\dell\\Desktop\\产品手册\\公司线上系统地址以及密码.docx";
        String fileName="123";
        String pdfPath="D:/test/"+fileName+".pdf";
        String excel2pdf="D:\\test\\747\\ZZCL_TYPE\\B类导入模板.xlsx";
        String pptpdf="D:\\test\\747\\ZZCL_TYPE\\诊改+数据中心介绍.pptx";
        //ppt2pdf(pptpdf,pdfPath);
         doc2pdf(filePaths, pdfPath);//filePaths需要转换的文件位置 pdfPath为存储位置

    }
}
